#!/bin/bash
#PBS -q batch 
#PBS -j oe 
#PBS -N wji
#PBS -l nodes=4:ppn=32
#PBS -l walltime=70:00:00

export OMP_NUM_THREADS=1
export MKL_NUM_THREADS=1
#module unload mpi/openmpi/1.6.3 
#module load mpi/mvapich2/1.9

cd $PBS_O_WORKDIR
cat $PBS_NODEFILE > ./host.txt
NPROCS=`cat $PBS_NODEFILE|wc -l`
 
dir_format="^[0-9]+x[0-9]+"
input_format="^[A-Za-z0-9]+\_[0-9.+\-]+\.in"
for dir in `ls ./`
do
  if [ -d $dir ]
  then
    if [[ $dir =~ $dir_format ]]
    then
      cd $dir
      mpirun -np $NPROCS -machinefile ../host.txt  /home/wji/work/soft/qe/qe-6.1/bin/pw.x  -npool 4 <input | tee output
      for file in `ls ./`
      do
        if [[ $file =~ $input_format ]]
        then
          name=`echo $file | sed 's#\(\.*\)\.in#\1\.out#' `
          mpirun -np $NPROCS -machinefile ../host.txt  /home/wji/work/soft/qe/qe-6.1/bin/pw.x  -npool 4 <$file | tee $name
        fi
      done
      cd ..
    fi
  fi
done
